package com.guigu.service.impl;

import com.alibaba.druid.filter.AutoLoad;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.ProductTypeMapper;
import com.guigu.pojo.ProductType;
import com.guigu.service.ProductTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商品类型表 服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Override
    public Page<ProductType> getProductType(HashMap<String, Object> map) {
        Integer currentPage = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("currentPage");
        if (pageNumObj != null) {
            if (pageNumObj instanceof String && StringUtils.isNotBlank((String) pageNumObj)) {
                currentPage = Integer.parseInt((String) pageNumObj);
            } else if (pageNumObj instanceof Integer) {
                currentPage = (Integer) pageNumObj;
            }
        }
        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null) {
            if (pageSizeObj instanceof String && StringUtils.isNotBlank((String) pageSizeObj)) {
                pageSize = Integer.parseInt((String) pageSizeObj);
            } else if (pageSizeObj instanceof Integer) {
                pageSize = (Integer) pageSizeObj;
            }
        }
        Page<ProductType> page = new Page<>(currentPage, pageSize);
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        if (map.get("typeName") != null && StringUtils.isNotBlank((String) map.get("typeName"))) {
            queryWrapper.like("type_name", map.get("typeName"));
        }
        if (map.get("level") != null && StringUtils.isNotBlank((String) map.get("level"))) {
            queryWrapper.like("level",map.get("level"));
        }

        Page<ProductType> productTypePage = productTypeMapper.selectPage(page, queryWrapper);

        return productTypePage;
    }

    @Override
    public List<ProductType> getProductTypeFirstLevel() {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level",1);
        List<ProductType> productTypes = productTypeMapper.selectList(queryWrapper);
        System.out.println(productTypes);
        return productTypes;
    }

    @Override
    public List<ProductType> getProductTypeSecondLevel() {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level",2);
        List<ProductType> productTypes = productTypeMapper.selectList(queryWrapper);
        System.out.println(productTypes);
        return productTypes;
    }

    @Override
    public List<ProductType> getAllProductType() {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","C001-1");
        if (productTypeMapper.selectList(queryWrapper)!=null){
            return productTypeMapper.selectList(queryWrapper);
        }
        return null;
    }

    @Override
    public List<ProductType> getAllProductTypeByLevel(Integer level) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","C001-1");
        queryWrapper.eq("level",level);
        if (productTypeMapper.selectList(queryWrapper)!=null){
            return productTypeMapper.selectList(queryWrapper);
        }
        return null;
    }
}
